package com.jt.provider.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * 基于此controller测试是否启用本地缓存的配置（这个配置从配置中心获取）
 */
@RefreshScope
@RestController
public class ProviderCacheController {
    @Value("${useLocalCache:false}")
    private boolean useLocalCache;

    @RequestMapping("/param/*/provider/cache01")
    public String doUseLocalCache01(){
        return "useLocalCache'value is   "+useLocalCache;
    }

    private static List<String> cache = new CopyOnWriteArrayList<>();
    @RequestMapping("/provider/cache02")
    public List<String> doUseLocalCache02(){
        //假设
        if(cache.isEmpty()) {//A,B,C,D
            synchronized (cache) {
                if (cache.isEmpty()) {
                    System.out.println("Get Data From Database");
                    List<String> data = Arrays.asList("A", "B", "C");cache.addAll(data);
                    }
                }
            }
            return cache;
        }

}
